package com.example.mydatasingleservice.services;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;

import com.example.mydatasingleservice.DataSingle;

public abstract class BaseServiceListenner extends Service implements LifecycleOwner {
    private String tag = "BaseServiceListenner";
    private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(tag, "onCreate: ");
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
        initLiveDataObserver();
    }

    protected void initLiveDataObserver() { }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(tag, "onStartCommand: ");
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(tag, "onBind: ");
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
        return null;
    }
    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(tag, "onUnbind: ");
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
        return super.onUnbind(intent);
    }
    @Override
    public void onDestroy() {
        Log.d(tag, "onDestroy: ");
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
        super.onDestroy();
    }

    @NonNull
    @Override
    public Lifecycle getLifecycle() {
        return mLifecycleRegistry;
    }
}
